using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PocketCargo
{
    class DeviceInfo
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct tagDeviceInfo
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
            public String tsManufacturer;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
            public String tsModel;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
            public String tsRevision;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
            public String tsSerialNumber;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
            public String tsSubscriberNumber;
        };

        [DllImport("cellcore.dll")]
        public static extern int lineGetGeneralInfo(IntPtr hLine, Byte[] lpLineGeneralInfo);

        public static String GetAppFileName()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
        }

        public static String GetAppDirName()
        {
            return System.IO.Path.GetDirectoryName(GetAppFileName());
        }

        public tagDeviceInfo GetDeviceInfo()
        {
            //tagDeviceInfo deviceInfo = new tagDeviceInfo();
            //GetExTAPIInfo(ref deviceInfo);
            OpenNETCF.Tapi.Tapi t = new OpenNETCF.Tapi.Tapi();
            t.Initialize();
            OpenNETCF.Tapi.Line l = t.CreateLine(0, OpenNETCF.Tapi.LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
            tagDeviceInfo gi = GetGeneralInfo(l);
            l.Dispose();
            t.Shutdown();
            return gi;
        }

        public String GetDeviceID()
        {
            tagDeviceInfo deviceInfo = GetDeviceInfo();
            return deviceInfo.tsSerialNumber; // +'*' + deviceInfo.tsSubscriberNumber;
        }

        public tagDeviceInfo GetGeneralInfo(OpenNETCF.Tapi.Line l)
        {
            tagDeviceInfo lgi=new tagDeviceInfo();
            Byte[] buffer = new Byte[512]; 
            
            //write size
             BitConverter.GetBytes(512).CopyTo(buffer, 0);

            if (lineGetGeneralInfo(l.hLine, buffer) != 0)           
            {
                throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error(), "TAPI Error: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString("X"));
            }

            int manusize = BitConverter.ToInt32(buffer, 12);
            int manuoffset = BitConverter.ToInt32(buffer, 16);
            lgi.tsManufacturer = System.Text.Encoding.Unicode.GetString(buffer, manuoffset, manusize);
            lgi.tsManufacturer = lgi.tsManufacturer.Substring(0, lgi.tsManufacturer.IndexOf('\x0'));

            int modelsize = BitConverter.ToInt32(buffer, 20);
            int modeloffset = BitConverter.ToInt32(buffer, 24);
            lgi.tsModel = System.Text.Encoding.Unicode.GetString(buffer, modeloffset, modelsize);
            lgi.tsModel = lgi.tsModel.Substring(0, lgi.tsModel.IndexOf('\x0'));

            int revsize = BitConverter.ToInt32(buffer, 28);
            int revoffset = BitConverter.ToInt32(buffer, 32);
            lgi.tsRevision = System.Text.Encoding.Unicode.GetString(buffer, revoffset, revsize);
            lgi.tsRevision = lgi.tsRevision.Substring(0, lgi.tsRevision.IndexOf('\x0'));

            int serialsize = BitConverter.ToInt32(buffer, 36);
            int serialoffset = BitConverter.ToInt32(buffer, 40);
            lgi.tsSerialNumber = System.Text.Encoding.Unicode.GetString(buffer, serialoffset, serialsize);
            lgi.tsSerialNumber = lgi.tsSerialNumber.Substring(0, lgi.tsSerialNumber.IndexOf('\x0'));

            int subscsize = BitConverter.ToInt32(buffer, 44);
            int subscoffset = BitConverter.ToInt32(buffer, 48);
            lgi.tsSubscriberNumber = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize);
            lgi.tsSubscriberNumber = lgi.tsSubscriberNumber.Substring(0, lgi.tsSubscriberNumber.IndexOf('\x0'));

            return lgi;
        }
    }
}
